//
//  HJUnlockCardController.m
//  Keping
//
//  Created by a on 2017/8/28.
//  Copyright © 2017年 柯平. All rights reserved.
//  解锁门禁卡

#import "HJUnlockCardController.h"
#import "HJUnlockCardCell.h"
#import "KLockAccessCardRequest.h"
#import "KEntranceCardsListModel.h"
#import "KUnlockCardModel.h"
#import "HJEntranceCardsListRequest.h"

@interface HJUnlockCardController ()<UITableViewDelegate,UITableViewDataSource,HJUnlockCardCellDelegate>
/**
 tableView
 */
@property(nonatomic,weak)UITableView*tableView;
@property (nonatomic, strong) KEntranceCardViewsModel *selectCardModel;
@property (nonatomic, strong) KEntranceCardsListModel *entranceCardsListModel;
@end

@implementation HJUnlockCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self getData];
}
-(void)setUI
{
    self.navigationItem.title=@"Unlock a Card";
    
    UITableView*tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.tableFooterView=[UIView new];
    [self.view addSubview:tableView];
    self.tableView=tableView;
}

-(void)getData
{
    [super getData];
    [self showLoading];
    
    HJEntranceCardsListRequest* listRequest = [HJEntranceCardsListRequest new];
    listRequest.params = @{@"status":@"N"};
    WEAKSELF;
    [listRequest startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        weakSelf.entranceCardsListModel = [KEntranceCardsListModel modelWithJSON:request.responseData];
        if (weakSelf.entranceCardsListModel.statusCode == 200) {
            [weakSelf.tableView reloadData];
        }else{
            [weakSelf showErrorText:weakSelf.entranceCardsListModel.msg errCode:weakSelf.entranceCardsListModel.statusCode];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        [weakSelf showError:request.error];
    }];
}

#pragma mark --UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.entranceCardsListModel.entranceCardViews.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJUnlockCardCell*cell = [HJUnlockCardCell cellWithTableView:tableView];
    cell.delegate = self;
    cell.entranceCardViews = self.entranceCardsListModel.entranceCardViews[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KEntranceCardViewsModel *model = self.entranceCardsListModel.entranceCardViews[indexPath.row];
    self.selectCardModel = model;
    [self clickUnlock];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return K_FactorH(90);
}
#pragma mark --HJUnlockCardCellDelegate
-(void)unlockCardCellClickUnlock:(KEntranceCardViewsModel *)entranceCardViews
{
    KLockAccessCardRequest* lockRequest = [[KLockAccessCardRequest alloc]initWithCarId:entranceCardViews.id];
    lockRequest.params = @{
                           @"startTime":entranceCardViews.startTime ? entranceCardViews.startTime : @"",
                           @"endTime":entranceCardViews.endTime? entranceCardViews.endTime : @"",
                           @"status":@"Y"
                           };
    WEAKSELF;
    [lockRequest startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [self hideHUD];
        //type1
        KUnlockCardModel*unlockModel = [KUnlockCardModel modelWithJSON:request.responseData];
        if (unlockModel.statusCode == 200) {
            [weakSelf showText:@"Sucess Unlock Your Card"];
            [weakSelf.navigationController popViewControllerAnimated:YES];

        }else{
            [weakSelf showErrorText:unlockModel.msg errCode:unlockModel.statusCode];
        }

    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [self hideHUD];
        [self showError:request.error];
    }];
}


-(void)clickUnlock
{
    NSString*title=[NSString stringWithFormat:@"Are you sure you want to unlock your card %@?",self.selectCardModel.cardNo];
    UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"Unlock a Card" message:title delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"Unlock", nil];
    [alert show];
}
#pragma mark --UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        [self unlockCardCellClickUnlock:self.selectCardModel];
    }
}

@end
